package com.jvtd.eyl.ui.main.home.read;

import android.content.Context;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.jvtd.eventBus.EventCenter;
import com.jvtd.eyl.R;
import com.jvtd.eyl.base.BaseMvpActivity;
import com.jvtd.eyl.bean.binding.BookBean;
import com.jvtd.eyl.constants.EventCode;
import com.jvtd.eyl.databinding.ActivityReadBinding;
import com.jvtd.eyl.ui.main.home.read.base.ReadBaseFragment;
import com.jvtd.eyl.ui.main.home.read.office.OfficeFragment;
import com.jvtd.eyl.ui.main.home.read.video.VideoFragment;
import com.jvtd.eyl.utils.ReadUtils;
import com.jvtd.eyl.widget.ReadViewPager;
import com.jvtd.widget.viewPager.pagerAdapter.JvtdFragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 预览界面
 *
 * 作者:chenlei
 * 时间:2018/11/15 9:37 AM
 */
public class ReadActivity extends BaseMvpActivity implements ReadViewPager.OnPageChangeListener {
    private ActivityReadBinding mBinding;
    private List<BookBean> mBookBeans;
    private int currentIndex;
    private List<Fragment> mFragments;
    private boolean isEnd = true;

    public static Intent getIntent(Context context) {
        return new Intent(context, ReadActivity.class);
    }

    @Override
    protected void initInject() {

    }

    @Override
    protected void getLayout() {
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_read);
    }

    @Override
    protected void initViewAndData() {
        mBookBeans = ReadUtils.getInstance().getBookBeans();
        currentIndex = ReadUtils.getInstance().getIndex();

        if (mBookBeans == null || mBookBeans.size() <= 0) {
            onError(R.string.error_message);
            return;
        }

        mFragments = new ArrayList<>();
        for (BookBean bookBean : mBookBeans) {
            if (TextUtils.equals(bookBean.getType(),BookBean.TYPE_VIDEO)) {
                mFragments.add(VideoFragment.newInstance(bookBean));
            }
            else {
                mFragments.add(OfficeFragment.newInstance(bookBean));
            }
        }
        initToolbar();

        JvtdFragmentPagerAdapter adapter = new JvtdFragmentPagerAdapter(getSupportFragmentManager(), mFragments);

        mBinding.viewPager.setAdapter(adapter);
        mBinding.viewPager.setCurrentItem(currentIndex);
        //图书太多造成OMM 必须注释
        mBinding.viewPager.setOffscreenPageLimit(0);
        mBinding.viewPager.setOnPageChangeListener(this);
        mBinding.viewPager.setScrollBarFadeDuration(2000);
    }

    private void initToolbar() {
        setToolbar(mBinding.toolBar.toolBar, true);
        updateToolbar();
    }

    private void updateToolbar() {
        mBinding.setTitleStr(mBookBeans.get(currentIndex).getName());
//        mBinding.toolBar.title.setText(mBookBeans.get(currentIndex).getName());
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mFragments == null || mFragments.size() <= 0){
            return super.onKeyDown(keyCode,event);
        }
        Fragment readBaseFragment = mFragments.get(currentIndex);
        if (readBaseFragment instanceof VideoFragment) {
            if (((VideoFragment) readBaseFragment).onKeyDownBack(keyCode, event)) {
                return super.onKeyDown(keyCode, event);
            }
            return false;
        }
        else if (readBaseFragment instanceof ReadBaseFragment)
            ((ReadBaseFragment) readBaseFragment).onKeyDown(keyCode,event);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        currentIndex = mBinding.viewPager.getCurrentItem();
        updateToolbar();
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void activityOnEvent(EventCenter eventCenter) {
        super.activityOnEvent(eventCenter);
        if (mBinding.viewPager == null) return;
        switch (eventCenter.getEventCode()){
            case EventCode.OPEN_SCROLL:
//                mBinding.viewPager.setScroll(true);
                break;
            case EventCode.BAN_SCROLL:
//                mBinding.viewPager.setScroll(false);
                break;
        }
    }
}
